The following is an extended example of how to create, and bundle, a simple java application which runs on top of a JRE included with it. It is worthwhile to note the directory structure of JRE components. The structure of the JRE is intentionally almost identical to that of the JDK (with some files missing and a few files renamed). The intention is to allow for a simple, seamless transition from developing an application with the JDK, to deploying it with the more-lightweight JRE. Feel free to take, use and modify any part of this example, such as source code or makefiles.
For more information on the Java Runtime itself, see its README file.
jre.demo.HelloWorld
. Here is the
HelloWorld.java source. We'll
refer to it a bit later. You can download the full HelloWorld
example for these platforms:
helloOnce your app works satisfactorily under the JDK, you're ready for step 2.
$(JRE_TOP)\bin
directory (next to all the java runtime's
dll's). On Solaris, you .so should be placed in the
$(JRE_TOP)/lib/$(ARCH)/$(THREADS_TYPE)
subdirectory,
where $(ARCH)
denotes the target architecture
(e.g., for this example, the architecture is "sparc"),
and $(THREADS_TYPE)
is the type of threads the
Solaris VM is using (for now, this must be "green_threads").
In the example programs, you'll see files called
$(JRE_TOP)\bin\HelloWorld.dll on win32 and
$(JRE_TOP)/lib/sparc/green_threads/HelloWorld.so
on Solaris. Please make sure the names of your native
libraries do not overwrite any of the JRE's native libraries.
public static void main(String[] argv)
method of your java class.
On Windows, if you have access to a C compiler during
development, a small C program is the best way to do
this. The sample program
hello.c will do nicely. You can compile this with a
macro defining JAVA_ARGS
to be the name
of your java class that defines main(String[] argv). An added
benefit of using a small C program like this one on Windows
is that, when compiled with -DWINMAIN
, you
can have a purely Windows application (that is, one that
does not pop up a console when started by double-clicking
on the desktop), if it is GUI-based.
On UNIX, a wrapper Korn-shell script like this can be used to invoke the java interpreter on your main class.
These Makefiles for win32 and Solaris were used to build the example programs. Both these simple wrapper programs should be located in the $(JRE_TOP)/bin directory. These wrappers look very much like the wrappers that invoke the java interpreter in the JDK. However, there's one main difference you should note:
The default CLASSPATH used by the JRE is:
$(JRE_TOP)/lib/rt.jar:$(JRE_TOP)/lib/i18n.jar:$(JRE_TOP)/lib/classes.jar:$(JRE_TOP)/lib/classes.zip:$(JRE_TOP)/classeswhere the variable
$(JRE_TOP)
is defined to be
the directory where the JRE is installed (it has 'bin' and 'lib'
directories underneath it). rt.jar and i18n.jar are used to hold the runtime's
(required) core classes and (optional) internationalization
classes, so it is recommended that you store the classes
specific to your application in either $(JRE_TOP)/lib/classes.jar,
or else as individual class files in the $(JRE_TOP)/classes
subdirectory.
String javaHome = System.getProperty("java.home"); /** This is the runtime directory path to the top of where * the java runtime is installed. It has the 'bin' and 'lib' * directories underneath it. */The mechanism is identical when your application is running on top of the JRE. In fact, our sample application HelloWorld.java uses this mechanism at rumtime to find the image it displays. The image is stored in the
lib
directory below
$(JRE_TOP) (or $(JAVA_HOME), the two are equivalent):
void loadImage() throws IOException { URL imgURL; String jh = System.getProperty("java.home"); String file = jh + File.separator+"lib"+ File.separator+"smooch.gif"; imgURL = new URL("file", "", file); .... }As an aside, the
lib
subdirectory is
the usual place to store and find such resources
(so long as the file names don't collide with any
of the files already there in the JRE).
Good luck! To submit comments or suggestions about the JRE, please send mail to the most appropriate engineering team from the list at JavaSoft email addresses.